package pl.integration.jdbcconnector.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;

import pl.integration.jdbcconnector.android.A_Pracownik;
import pl.integration.jdbcconnector.android.A_Uczen;
import pl.integration.jdbcconnector.android.A_Wiadomosc;
import pl.integration.jdbcconnector.exceptions.NotFoundInDbException;
import pl.integration.jdbcconnector.objects.Klasa;
import pl.integration.jdbcconnector.objects.Notatka;
import pl.integration.jdbcconnector.objects.Pracownik;
import pl.integration.jdbcconnector.objects.Uczen;
import pl.integration.jdbcconnector.objects.Wiadomosc;

public class AndroidDaoImpl extends HibernateTemplate implements AndroidDao {

    @Override
    public A_Pracownik getPracownik(final String login)
            throws NotFoundInDbException {

        List found = this.find("from Pracownik where login = ?", login);
        if (found.size() < 0) {
            throw new NotFoundInDbException();
        }
        if (found.get(0) instanceof Pracownik) {
            return new A_Pracownik((Pracownik) found.get(0), this);
        }
        return null;
    }

    @Override
    public List<Notatka> getNotatki(int id) throws NotFoundInDbException {
        List found = this.find("from Notatka where pracownikId = ?",
                Integer.valueOf(id));
        if (found.size() < 0) {
            throw new NotFoundInDbException();
        }
        return found;
    }

    public AndroidDaoImpl() {
        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.dialect",
                "pl.integration.jdbcconnector.SQLiteDialect");
        cfg.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC");
        cfg.setProperty("hibernate.connection.url",
                "jdbc:sqlite:e:/database.db");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");
        cfg.setProperty("show_sql", "true");
        cfg.setProperty("format_sql", "true");

        cfg.addResource("hbms/Notatka.hbm.xml");
        cfg.addResource("hbms/Pracownik.hbm.xml");
        cfg.addResource("hbms/Komunikat.hbm.xml");
        cfg.addResource("hbms/Klasa.hbm.xml");
        cfg.addResource("hbms/Uczen.hbm.xml");
        cfg.addResource("hbms/Rodzic.hbm.xml");
        cfg.addResource("hbms/Przedmiot.hbm.xml");
        this.setSessionFactory(cfg.buildSessionFactory());
    }

    @Override
    public List<A_Uczen> getUczniowie(Klasa klasa) throws NotFoundInDbException {

        List found = this.find("from Uczen where klasa = ?", klasa);
        if (found.size() < 0) {
            throw new NotFoundInDbException();
        }
        List<A_Uczen> ucz = new LinkedList<A_Uczen>();
        if (found.get(0) instanceof Uczen) {
            for (Object o : found) {
                ucz.add(new A_Uczen((Uczen) o));
            }
            return ucz;
        }
        return null;
    }

    @Override
    public List<A_Wiadomosc> getWiadomosci(int user)
            throws NotFoundInDbException {

        List found = this.find("from Wiadomosc where nadawca = ?", user);
        if (found.size() < 0) {
            throw new NotFoundInDbException();
        }
        List<A_Wiadomosc> list = new LinkedList<A_Wiadomosc>();
        if (found.get(0) instanceof Wiadomosc) {
            for (Wiadomosc w : (List<Wiadomosc>) found) {
                List prac = this.find("from Pracownik where pracownikId =?",
                        w.getNadawca());
                A_Wiadomosc aWiad = new A_Wiadomosc(w, (Pracownik) prac.get(0));
                list.add(aWiad);
            }
        }

        return null;
    }

}
